
from django.views import View
from django.db import transaction

from User.models import OrderVip, OrderGoods
from Venue.models import Vip, Goods
from sportsground.utils import Req
from sportsground.utils.Res import Res
from sportsground.utils.views import LoginRequiredUser


class GoodsView(LoginRequiredUser, View):
    """ 商品订单 """

    @Req.getJson
    def post(self, request):
        """ 添加 """
        goodsId = request.json_dict.get('goodsId')
        number = request.json_dict.get('number',1)
        message = request.json_dict.get('message','')

        user = request.user.user

        with transaction.atomic():
            goods = Goods.objects.select_for_update().filter(id=goodsId).first()
            if not goods:
                return Res.Json(Res.ParamErr)

            order = OrderGoods.create(user, goods=goods,number=number, message=message)
            return Res.Json(Res.OK, result=order.Info())
